package com.caogen.cg_wl_kt.base

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.caogen.cg_wl_kt.model.dashboard.DashboardBean

/**
 * 项目名称: cg_wl_kt
 * Packages:com.caogen.cg_wl_kt.base
 * author: FSW
 * 创建日期: 2024/2/28 16:24
 * Describe:
 *
 */
abstract class BaseAdapter<T, VB : ViewBinding>(
    val inflater: (LayoutInflater, parent: ViewGroup, attachToRoot: Boolean) -> VB
) : RecyclerView.Adapter<BaseHolder<VB>>() {
    var data: MutableList<T> = mutableListOf()

    //ItemClick事件
    var mItemClickListener: OnItemClickListener<T>? = null
    /*
        ItemClick事件声明
     */
    interface OnItemClickListener<in T> {
        fun onItemClick(item: T, position: Int)
    }
    fun setOnItemClickListener(listener: OnItemClickListener<T>) {
        this.mItemClickListener = listener
    }
    fun setListData(sources: MutableList<T>) {
        data = sources
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseHolder<VB> {
        return BaseHolder(inflater(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: BaseHolder<VB>, position: Int) {
        holder.itemView.setOnClickListener {
            if (mItemClickListener != null)
                mItemClickListener!!.onItemClick(data[position], position)
        }
        onBindingData(holder, data[position], position);
    }

    protected abstract fun onBindingData(holder: BaseHolder<VB>?, t: T, position: Int)

}
